'''
题目链接：
https://leetcode-cn.com/problems/string-to-integer-atoi/

'''
import re

# 第一次实践
class Solution:
    def myAtoi(self, str: str) -> int:
        num = 0
        # 正则匹配内容
        str_num = re.match(r"^[\-\+]?\d+",str.lstrip())
        if str_num:
            str_num = str_num.group()
            num = max(min(int(str_num),2**31-1),-2**31)
        return num

# 测试
obj = Solution()
test = obj.myAtoi

s1 = "s   -42"
assert test(s1) == 0

s2 = "   -42"
assert test(s2) == -42

s3 = "4193 with words"
assert test(s3) == 4193

s4 = "-91283472332"
assert test(s4) == -2**31